package impl

import (
	"user-server/src/boot"
	"user-server/src/model"
	"user-server/src/repo"
)

func init() {
	repo.InjectUserInfoDao(&UserInfoDaoImpl{DbEngine: boot.GetOrmEngine()})
}

type UserInfoDaoImpl struct {
	DbEngine *boot.Orm
}

func (ud *UserInfoDaoImpl) QueryUserByUsername(username string) (*model.UserInfo, error) {
	u := &model.UserInfo{Username: username}
	if get, err := ud.DbEngine.Get(u); get {
		return u, err
	}
	return nil, nil
}

func (ud *UserInfoDaoImpl) UpdateByUsername(username, nickname, profile string) (bool, error) {
	condition := &model.UserInfo{Username: username}
	userInfo := &model.UserInfo{}
	if nickname != "" {
		userInfo.Nickname = nickname
	}
	if profile != "" {
		userInfo.Profile = profile
	}
	update, err := ud.DbEngine.Update(userInfo, condition)
	return update == 1, err
}
